package com.hongyuan.sellfitness.ui.home.view_mode;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.RxBus;
import com.hongyuan.mvvmhabitx.bus.RxSubscriptions;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.data.rxbus.ChangeStoreMessage;
import com.hongyuan.sellfitness.ui.home.activity.StoreListActivity;
import com.hongyuan.sellfitness.ui.home.beans.StoreInfoBean;
import com.hongyuan.sellfitness.ui.home.beans.StorePriviteLessonBean;
import com.hongyuan.sellfitness.ui.main.beans.AuthorityBean;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class StorePriviteLessonViewModel extends CustomViewModel<MyRepository> {

    private Disposable mSubscription;

    public StorePriviteLessonViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //权限管理
    public ObservableField<AuthorityBean> authority = new ObservableField<>();
    //数据源
    public ObservableField<StoreInfoBean> entity = new ObservableField<>();

    //给RecyclerView添加ObservableList
    public ObservableList<StoreLessonItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<StoreLessonItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_store_privite_lesson);

    //门店列表
    public BindingCommand goStoreList = new BindingCommand(() -> startActivity(StoreListActivity.class));

    @Override
    public void onCreate() {
        super.onCreate();

        mSubscription = RxBus.getDefault().toObservable(ChangeStoreMessage.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(progressLoadBean -> {
                    curPage = FIRST_PAGE;
                    //更新下数据
                    clearParams().setParams("os_id",progressLoadBean.getOs_id()).setParams("os_ids",progressLoadBean.getOs_id());
                    requestData(Constants.GET_OFFLINE_STORE_INFO);

                    requestData(Constants.GET_COURSE_PRIVITE_LIST);
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        //取消订阅，防止内存泄漏
        RxSubscriptions.remove(mSubscription);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.VERIFY_ROLE){
            AuthorityBean bean = (AuthorityBean)dataBean;
            //设定死的，权限功能服务端还有问题
            bean.setState(1);
            authority.set(bean);

            if(bean.getState() == 1){
                //请求门店信息
                requestData(Constants.GET_OFFLINE_STORE_INFO);
                //请求私教课
                requestData(Constants.GET_COURSE_PRIVITE_LIST);
            }
        }

        if(code == Constants.GET_OFFLINE_STORE_INFO){
            StoreInfoBean infoBean = (StoreInfoBean)dataBean;

            entity.set(infoBean);
        }

        if(code == Constants.GET_COURSE_PRIVITE_LIST){

            List<StorePriviteLessonBean.ListBean> list = ((StorePriviteLessonBean)dataBean).getList();

            if(list != null && list.size() > 0){
                if(curPage == FIRST_PAGE){
                    observableList.clear();
                    addBinding(list);
                }else{
                    addBinding(list);
                }
            }

            if(observableList.size() > 0){
                if(list == null || list.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }
        }
    }

    /*
     * 添加布局显示
     * */
    private void addBinding(List<StorePriviteLessonBean.ListBean> list){
        for(StorePriviteLessonBean.ListBean bean : list){
            StoreLessonItemViewModel itemViewModel = new StoreLessonItemViewModel(this,bean);
            observableList.add(itemViewModel);
        }
    }
}
